package com.smartscreen.gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GatewayConfig {

    /**
     * 配置CORS
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.addAllowedOriginPattern("*");
        corsConfig.addAllowedMethod("*");
        corsConfig.addAllowedHeader("*");
        corsConfig.setAllowCredentials(true);
        corsConfig.setMaxAge(3600L);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig);

        return new CorsWebFilter(source);
    }

    /**
     * 自定义路由配置
     */
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                // 认证管理服务
                .route("auth-service", r -> r
                        .path("/api/auth/**")
//                        .filters(f -> f.stripPrefix(2)) // 移除 /api/auth 前缀
                        .uri("http://localhost:8000"))
                // 基础信息管理服务
                .route("base-info-service", r -> r
                        .path("/api/base/**")
//                        .filters(f -> f.stripPrefix(2)) // 移除 /api/base 前缀
                        .uri("http://localhost:8080"))
                
                // 内容管理服务
                .route("content-service", r -> r
                        .path("/api/content/**")
//                        .filters(f -> f.stripPrefix(2))
                        .uri("http://localhost:8081"))
                
                // 课程安排管理服务
                .route("schedule-service", r -> r
                        .path("/api/schedule/**")
//                        .filters(f -> f.stripPrefix(2))
                        .uri("http://localhost:8082"))
                
                // 考勤管理服务
                .route("attendance-service", r -> r
                        .path("/api/attendance/**")
//                        .filters(f -> f.stripPrefix(2))
                        .uri("http://localhost:8083"))
                
                // 成绩管理服务
                .route("grade-service", r -> r
                        .path("/api/grade/**")
//                        .filters(f -> f.stripPrefix(2))
                        .uri("http://localhost:8084"))
                
                // 家校联系服务
                .route("home-school-service", r -> r
                        .path("/api/home-school/**")
//                        .filters(f -> f.stripPrefix(2))
                        .uri("http://localhost:8085"))
                
                // 生活服务信息管理服务
                .route("life-service-service", r -> r
                        .path("/api/life-service/**")
//                        .filters(f -> f.stripPrefix(2))
                        .uri("http://localhost:8086"))
                
                .build();
    }
}